<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests\AdminloginRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeUserController extends Controller
{
	//用户管理部分
    //前台用户编辑页面
	public function getEdit()
    {
        // dd($request->all());
        //查询
		$users = DB::table('user')->where('id',session('id'))->first();
		// dd($users);
		//解析模板
        return view('home.user.edit',['user'=> $users]);
    }
	//前台用户修改操作
	public function postUpdate(Request $request)
	{
		// dd($request->all());
		// dd($request->hasFile('pic'));
        $this->validate($request, [
            'username' => 'required|max:15',
            'truename' => 'required|max:12',
            'email' => 'required|email',
            'phone' => 'required|regex:{1\d{10}}',
            'age' => 'required|regex:{\d{1,3}}|max:3',
        ],[
            'username.required' => '用户名不能为空',
            'username.max' => '用户名必须小于15个字符',
            'truename.required' => '真实姓名不能为空',
            'truename.max' => '真实姓名必须小于12个字符',
            'email.required' => '邮箱不能为空',
            'email.email'=>'邮箱格式不正确',
            'phone.required' => '手机号不能为空',
            'phone.regex' => '手机号格式不正确',
            'age.required' => '年龄不能为空',
            'age.regex' => '年龄格式不正确,请输入数字',
            'age.max' => '年龄不能超过3位数',
        ]);
        // dd($request->id);
        $id = $request->only('id');
        $data = $request->only(['username','truename','email','phone','sex','age','address']);
        //检测文件是否上传
        if($request->hasFile('pic')){
            //如果上传了
            //随机文件名
            $name = md5(time()+rand(1,999999));
            //获取后缀名
            $su = $request->file('pic')->getClientOriginalExtension();
            $arr = array('jpg','png','jpeg','tiff');
            //判断
            if(!in_array($su,$arr)){
                // die('非法上传');
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://lamp154.cn/test/add";
                </script>';
            }
            //将文件移动到指定目录下
            $request->file('pic')->move('./upload/', $name.'.'.$su);
            $data['pic'] = $name.'.'.$su;
        }else{
            $data['pic'] = $request->pic;
        }
        // dd($data);
        //数据库修改
		$res = DB::table('user')->where('id',$id)->update($data);
        // dd($res);
		//判断
		if($res){
			return back()->with('success','用户修改成功');
		}else{
			return back()->with('error','用户修改失败');
		}
	}
    //用户中心首页
    public function getIndex()
    {
        return view('home.user.index');
    }
    //地址管理部分
    //地址列表管理
    public function getAddress()
    {
        //查询地址信息
        $addresses = DB::table('address')->where('user_name',session('name'))->get();
        // dd($addresses);
        //解析模板
        return view('home.user.address',['addresses'=>$addresses]);
    }
    //添加地址管理
    public function getAddinsert(Request $request)
    {
        // dd($request);
        // dd($request->all());
        $this->validate($request, [
            'content' => 'required',
            'postcode' => 'required|regex:{\d{6}}',
            'dizhi_name' => 'required|max:25',
            'phone' => 'required|regex:{1\d{10}}',
        ],[
            'content.required' => '详细地址不能为空',
            'postcode.required' => '邮政编码不能为空',
            'postcode.regex' => '邮政编码必须为6位数字',
            'dizhi_name.required' => '收货姓名不能为空',
            'dizhi_name.max' => '收货姓名不超过25个字符',
            'phone.required' => '收货手机号不能为空',
            'phone.regex' => '收货手机号格式不正确',
        ]);
        $data = $request->all();
        $data['user_name'] = session('name');
        if(!empty($data['statuss'])){
            $data['statuss'] = 2;
            //修改原始默认地址为一般地址
            $dataa['statuss'] = 1;
            DB::table('address')
            ->where(function ($query){
                $query->where('user_name',session('name'))
                      ->where('statuss',2);
            })
            ->update($dataa);
        }else{
            $data['statuss'] = 1;
        }
        // dd($data);
        //添加
        $res = DB::table('address')->insert($data);
        //判断添加是否成功
        if($res){
            return redirect('/home/user/address')->with('success','地址添加成功');
        }else{
            return back()->with('error','地址添加失败');
        }
    }
    //地址修改
    public function getAddressedit($id)
    {
        // dd($id);
        //查询
        $addresse = DB::table('address')->where('id',$id)->first();
        // dd($addresse);
        //解析模板
        return view('home.user.addressedit',['addresse'=> $addresse]);
    }
    //执行修改
    public function getAddressupdate(Request $request)
    {
        // dd($request->all());
        $this->validate($request, [
            'content' => 'required',
            'postcode' => 'required|regex:{\d{6}}',
            'dizhi_name' => 'required|max:25',
            'phone' => 'required|regex:{1\d{10}}',
        ],[
            'content.required' => '详细地址不能为空',
            'postcode.required' => '邮政编码不能为空',
            'postcode.regex' => '邮政编码必须为6位数字',
            'dizhi_name.required' => '收货姓名不能为空',
            'dizhi_name.max' => '收货姓名不超过25个字符',
            'phone.required' => '收货手机号不能为空',
            'phone.regex' => '收货手机号格式不正确',
        ]);
        $data = $request->except('id');
        if(!empty($data['statuss'])){
            $data['statuss'] = 2;
            //修改原始默认地址为一般地址
            $dataa['statuss'] = 1;
            DB::table('address')
            ->where(function ($query){
                $query->where('user_name',session('name'))
                      ->where('statuss',2);
            })
            ->update($dataa);
        }else{
            $data['statuss'] = 1;
        }
        // dd($data);
        //修改
        $res = DB::table('address')->where('id',$request->id)->update($data);
        //判断修改是否成功
        if($res){
            return redirect('/home/user/address')->with('success','地址修改成功');
        }else{
            return back()->with('error','地址修改失败');
        }
    }
    //地址删除
    public function postAddressdelete(Request $request)
    {
        $id = $request->input('id');
        $res = DB::table('address')->where('id',$id)->delete();
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }
    //密码修改部分
    //修改界面视图
    public function getPassword()
    {
        return view('home.user.password');
    }
    //检测原始密码是否正确
    public function postPasstest(Request $request)
    {
        $res = DB::table('user')->where('id',session('id'))->first();
        // 检测原始密码是否一致
        if(Hash::check($request->input('oldpass'),$res->password)){
            //密码一致
            echo 1;
        }else{
            // 密码不一致
            echo 2;
        }
    }
    //前台密码修改操作
    public function postPassupdate(Request $request)
    {
        // dd($request->all());
        // dd($request->session());
        $data['password'] = $request->input('newpass');
        $data['password'] = Hash::make($data['password']);
        //数据库修改
        $res = DB::table('user')->where('id',session('id'))->update($data);
        // dd($res);
        //判断
        if($res){
            $request->session()->forget('name');
            $request->session()->forget('id');
            return redirect('/home/login/login')->with('error','密码修改成功,请重新登录');
        }else{
            return back()->with('error','密码修改失败');
        }
    }
}

?>